package com.neusoft.neuiotms.dcom.converter;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.MaintainTaskDTO;
import com.neusoft.neuiotms.dcom.model.MaintainPlan;
import com.neusoft.neuiotms.dcom.model.MaintainRule;
import com.neusoft.neuiotms.dcom.model.MaintainTask;
import com.neusoft.neuiotms.dcom.model.enums.MaintainTaskSourceEnum;
import com.neusoft.neuiotms.dcom.model.enums.MaintainTaskStatusEnum;
import com.neusoft.neuiotms.dcom.model.enums.StopServiceNoticeSendStatusEnum;
import com.neusoft.neuiotms.dcom.repository.MaintainPlanRepository;
import com.neusoft.neuiotms.dcom.repository.MaintainRuleRepository;
import com.neusoft.neuiotms.dcom.repository.MaintainTaskRepository;
import com.neusoft.neuiotms.dcom.vo.MaintainTaskQueryExportVO;

import io.micrometer.core.instrument.util.StringUtils;

@Component
public class MaintainTaskConverter extends AbstractConverter<MaintainTask, MaintainTaskDTO> {

    @Autowired
    private MaintainTaskRepository maintainTaskRepository;
    @Autowired
    private MaintainPlanRepository maintainPlanRepository;
    @Autowired
    private BaseIdGenerator maintainTaskCodeGenerator;
    @Autowired
    private MaintainRuleRepository maintainRuleRepository;

    @Override
    public MaintainTask toModel(final MaintainTaskDTO dto) {
        MaintainTask model;
        if (dto.isNew()) {
            model = new MaintainTask();
            model.setCreateTime(new Date());
        } else {
            model = this.maintainTaskRepository.getOne(dto.getId());
            model.setUpdateTime(new Date());
        }
        model.setCode(
                StringUtils.isEmpty(dto.getCode()) ? this.maintainTaskCodeGenerator.generateCode() : dto.getCode());
        model.setName(dto.getName());

        if (StringUtils.isNotEmpty(dto.getPlanCode())) {
            final MaintainPlan plan = this.maintainPlanRepository.findByCode(dto.getPlanCode());
            model.setPlan(plan);
        }

        model.setModelCode(dto.getModelCode());
        model.setModelName(dto.getModelName());

        model.setSuspendService(dto.isSuspendService());
        model.setQuantifyingTime(dto.getQuantifyingTime());

        model.setStatus(dto.getStatus());

        if (dto.getRule() != null) {
            model.setRule(dto.getRule());
        } else if (StringUtils.isNotBlank(dto.getRuleCode())) {
            final MaintainRule rule = this.maintainRuleRepository.getByCode(dto.getRuleCode());
            model.setRule(rule);
        }

        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());

        model.setObjCode(dto.getObjCode());
        model.setObjName(dto.getObjName());

        model.setChargeUserId(dto.getChargeUserId());
        model.setChargeUserCode(dto.getChargeUserCode());
        model.setChargeUserName(dto.getChargeUserName());

        model.setStartDateTime(dto.getStartDateTime());
        model.setEndDateTime(dto.getEndDateTime());

        model.setResult(dto.getResult());
        model.setJoinUser(dto.getJoinUser());
        model.setTel(dto.getTel());
        model.setNoticeSendStatus(dto.getNoticeSendStatus());
        model.setResultDesc(dto.getResultDesc());
        model.setProcessDesc(dto.getProcessDesc());
        model.setRemarks(dto.getRemarks());
        model.setCreateRule(dto.isCreateRule());
        model.setSource(dto.getSource());

        model.setCreateTime(dto.getCreateTime());
        model.setUpdateTime(dto.getUpdateTime());

        model.setVersion(dto.getVersion());

        model.setJsonObjs(dto.getJsonObjs());
        return model;
    }

    @Override
    public MaintainTaskDTO toDTO(final MaintainTask model, final boolean forListView) {
        final MaintainTaskDTO dto = new MaintainTaskDTO();

        dto.setId(model.getId());

        dto.setCode(model.getCode());
        dto.setName(model.getName());

        if (model.getPlan() != null) {
            dto.setPlanCode(model.getPlan().getCode());
            dto.setPlanName(model.getPlan().getName());
            dto.setPlanVersion(model.getPlan().getVersion());
        }

        if (model.getRule() != null) {
            dto.setRule(model.getRule());
            dto.setRuleId(model.getRule().getId());
            dto.setRuleName(model.getRule().getName());
            dto.setRuleCode(model.getRule().getCode());
            if (model.isCreateRule()) {
                dto.setDescription(model.getRule().getDescription());
            }
        }

        dto.setModelCode(model.getModelCode());
        dto.setModelName(model.getModelName());

        dto.setSuspendService(model.isSuspendService());
        dto.setQuantifyingTime(model.getQuantifyingTime());
        dto.setStatus(model.getStatus());

        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());

        dto.setObjCode(model.getObjCode());
        dto.setObjName(model.getObjName());

        dto.setChargeUserId(model.getChargeUserId());
        dto.setChargeUserCode(model.getChargeUserCode());
        dto.setChargeUserName(model.getChargeUserName());

        dto.setStartDateTime(model.getStartDateTime());
        dto.setEndDateTime(model.getEndDateTime());

        dto.setResult(model.getResult());
        dto.setJoinUser(model.getJoinUser());
        dto.setTel(model.getTel());
        dto.setNoticeSendStatus(model.getNoticeSendStatus());
        dto.setResultDesc(model.getResultDesc());
        dto.setProcessDesc(model.getProcessDesc());
        dto.setRemarks(model.getRemarks());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        dto.setCreateRule(model.isCreateRule());
        dto.setSource(model.getSource());

        dto.setJsonObjs(model.getJsonObjs());

        dto.setVersion(model.getVersion());
        return dto;
    }

    public MaintainTaskQueryExportVO toMaintainTaskQueryVO(final MaintainTask model) {
        final MaintainTaskQueryExportVO vo = new MaintainTaskQueryExportVO();

        vo.setCode(model.getCode());
        vo.setName(model.getName());
        if (model.getPlan() != null) {
            vo.setPlanName(model.getPlan().getName());
        }
        if (model.getSource() != null) {
            if (model.getSource().name().equals(MaintainTaskSourceEnum.temporary.name())) {
                vo.setSourceFrom(MaintainTaskSourceEnum.temporary.getLabel());
            } else {
                vo.setSourceFrom(MaintainTaskSourceEnum.period.getLabel());
            }

        }
        vo.setModelName(model.getModelName());
        vo.setObjName(model.getObjName());
        vo.setOrgName(model.getOrgName());
        if (model.getRule() != null) {
            vo.setRuleName(model.getRule().getName());
        }
        vo.setChargeUserName(model.getChargeUserName());

        if (MaintainTaskStatusEnum.undo.name().equals(model.getStatus().name())) {
            vo.setStatus(MaintainTaskStatusEnum.undo.getLabel());
        }
        if (MaintainTaskStatusEnum.done.name().equals(model.getStatus().name())) {
            vo.setStatus(MaintainTaskStatusEnum.done.getLabel());
        }
        if (MaintainTaskStatusEnum.doing.name().equals(model.getStatus().name())) {
            vo.setStatus(MaintainTaskStatusEnum.doing.getLabel());
        }

        if (model.isSuspendService()) {
            vo.setSuspendService("是");
        } else {
            vo.setSuspendService("否");
        }

        if (model.getNoticeSendStatus() != null) {
            if (StopServiceNoticeSendStatusEnum.send.name().equals(model.getNoticeSendStatus().name())) {
                vo.setNoticeSendStatus(StopServiceNoticeSendStatusEnum.send.getLabel());
            }
            if (StopServiceNoticeSendStatusEnum.unsend.name().equals(model.getNoticeSendStatus().name())) {
                vo.setNoticeSendStatus(StopServiceNoticeSendStatusEnum.unsend.getLabel());
            }
            if (StopServiceNoticeSendStatusEnum.discard.name().equals(model.getNoticeSendStatus().name())) {
                vo.setNoticeSendStatus(StopServiceNoticeSendStatusEnum.discard.getLabel());
            }
        }

        vo.setQuantifyingTime(model.getQuantifyingTime());
        vo.setStartDateTime(model.getStartDateTime());
        vo.setEndDateTime(model.getEndDateTime());
        vo.setCreateTime(model.getCreateTime());
        return vo;
    }

}
